package com.zh.yida.service.Impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zh.yida.common.BusinessException;
import com.zh.yida.entity.Category;
import com.zh.yida.entity.Dish;
import com.zh.yida.entity.Setmeal;
import com.zh.yida.mapper.CategoryMapper;
import com.zh.yida.service.CategoryService;
import com.zh.yida.service.DishService;
import com.zh.yida.service.SetmealService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * 自定义判断逻辑
 */
@Service
public class CategoryServiceImpl extends ServiceImpl<CategoryMapper, Category> implements CategoryService {

    @Autowired
    private DishService dishService;

    @Autowired
    private SetmealService setMealService;

    public void categoryDel(Long id) {
        LambdaQueryWrapper<Dish> lqwDish = new LambdaQueryWrapper<Dish>();
        lqwDish.eq(Dish::getCategoryId, id);
        int countDish = dishService.count(lqwDish);
        if (countDish > 0) {
            throw new BusinessException("当前分类关联了菜品，不能删除");
        }

        LambdaQueryWrapper<Setmeal> lqwSetMeal = new LambdaQueryWrapper<>();
        lqwSetMeal.eq(Setmeal::getCategoryId, id);
        int countSetMeal = setMealService.count(lqwSetMeal);
        if (countSetMeal > 0) {
            throw new BusinessException("当前分类关联了套餐，不能删除");
        }

        super.removeById(id);
    }

}
